﻿using Application.AppService.CFSKAppService;
using Application.AppService.CPCAppService;
using Application.AppService.ERAppService;
using Application.AppService.HFAppService;
using AutoMapper;
using Domain;
using Domain.Entity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace UI.Controllers.Human_resource_file_management_人力资源档案管理_
{
	public class HFController : Controller
	{
		private readonly IMapper mapper;
		private readonly IHFAppService hFAppService;
		private readonly IWebHostEnvironment webHostEnvironment;

		public HFController(IMapper mapper, IHFAppService hFAppService,IWebHostEnvironment webHostEnvironment)
		{
			this.mapper = mapper;
			this.hFAppService = hFAppService;
			this.webHostEnvironment = webHostEnvironment;
		}
		public IActionResult Index()
		{
			return View();
		}
        public IActionResult Index1()
        {
            return View();
        }
        [HttpPost]
		public IActionResult Upload(IFormFileCollection file)
		{
			string name1 = "";
			foreach (var item in file)
			{
				//生成文件名
				string name = DateTime.Now.ToString("yyyyMMddHHmmssfff");
				//获取后缀名
				string ext = item.FileName.Substring(item.FileName.LastIndexOf('.'));
				//设置上传路径
				//string path = $"Uploader/{DateTime.Now.ToString("yyyy/MM/dd/")}" + name1;
				string path = $"wwwroot/images/" + name + ext;
				//Core生成文件的绝对路径
				var jpath = Path.Combine(webHostEnvironment.ContentRootPath, path);
				//创建上传文件对应的文件夹
				(new FileInfo(jpath)).Directory.Create();
				using (FileStream stream = new FileStream(jpath, FileMode.CreateNew))
				{
					item.CopyTo(stream);//图片上传
										//TempData["name"] = name + ext;//保存图片的值
				}
				name1 = name + ext;

			}
			return Content(name1);
		}
		[HttpPost("AddHF")]
		public async Task<IActionResult> AddHF(HF hf)
		{

			bool rs =await hFAppService.AddHFAsync(hf);
			return Json(rs);
		}
		public async Task<IActionResult> UpdateHFMC (HFUpdateDto  hFUpdateDto)
		{
			HF hF = mapper.Map<HF>(hFUpdateDto);
			bool rs = await hFAppService.UpdateHFMC(hF);
			return Json(rs);
		}
	}
}
